package cn.maxhou.sl6.config;

import cn.maxhou.sl1.entity.SysRole;
import cn.maxhou.sl1.entity.SysUser;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

//定义配置类
@Configuration
//指定读取外部配置文件,value指定路径,encoding指定读取时的字符编码
@PropertySource(value="classpath:user.properties",encoding = "UTF-8")
public class JavaConfig {
    //使用@Value注解可以读取配置文件中的指定值
    @Value("${user.realName}")
    private String realName;
    @Value("${user.account}")
    private String account;
    @Value("${user.password}")
    private String password;
    @Value("${user.sex}")
    private int sex;
    //注册"管理员"角色bean对象
    @Bean
    public SysUser user(@Qualifier("adminRole")SysRole role) {
        SysUser user = new SysUser();
        user.setRealName(realName);
        user.setAccount(account);
        user.setPassword(password);
        user.setSex(sex);
        user.setRole(role);

        //返回对象会被注册道Spring容器中
        return user;
    }

    //注册"管理员"角色bean对象
    @Bean
    @Primary
    public SysRole adminRole(){
        SysRole role = new SysRole();
        role.setId(1);
        role.setRoleName("管理员");
        role.setCode("ADMIN");
        return role;
    }
}
